package com.wgq.uitest.exp3;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;

import com.wgq.uitest.R;

public class MyProgressDialog extends AppCompatActivity {

    private int[] data = new int[100];
    int hasData = 0;

    int progressStatus = 0;
    ProgressDialog progressDialog;
    /**
     * 定义一个handler
     */
    Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 0x112:
                    progressDialog.setProgress(progressStatus);   //更新进度条
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_progress_dialog);

        Button btn = (Button) findViewById(R.id.exec);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDialog(0x112);
            }
        });
    }

    /**
     * 打开对话框
     * @param id
     * @param args
     * @return
     */
    @Nullable
    @Override
    protected Dialog onCreateDialog(int id, Bundle args) {
        switch (id){
            case 0x112:
                progressDialog = new ProgressDialog(this);
                progressDialog.setMax(100);
                progressDialog.setTitle("任务完成百分比");
                progressDialog.setMessage("耗时任务的完成百分比");
                progressDialog.setCancelable(false);
                progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                progressDialog.setIndeterminate(false);
                break;
        }
        return progressDialog;
    }

    @Override
    protected void onPrepareDialog(int id, Dialog dialog) {
        super.onPrepareDialog(id, dialog);
        switch (id){
            case 0x112:
                progressDialog.incrementProgressBy(-progressDialog.getProgress());
                new Thread(){
                    public void run(){
                        while(progressStatus < 100){
                            progressStatus = doWork();
                            handler.sendEmptyMessage(0x112);
                        }
                        if(progressStatus >= 100){
                            progressDialog.dismiss();
                        }
                    }
                }.start();
                break;
        }
    }

    /**
     * 耗时函数
     * @return
     */
    public int doWork(){
        data[hasData++] = (int)(Math.random() * 100);
        try{
            Thread.sleep(100);
        } catch (InterruptedException e){
            e.printStackTrace();
        }
        return hasData;
    }
}
